package com.gzy.android_douban.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author : guzy
 * @program: net-app
 * @description:
 * @date : 2022-03-24 13:06
 **/
public class BaseFragment<T extends ViewBinding> extends Fragment {
  protected T binding;
  public final String TAG = this.getClass().getSimpleName();
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    Type superclass = getClass().getGenericSuperclass();
    Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
    try {
      Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);
      binding = (T) method.invoke(null, getLayoutInflater(),container,false);
    } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
      e.printStackTrace();
    }
    return binding.getRoot();
  }
}